package com.cn.yunhe.rentcars.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.CarMapper;
import com.cn.yunhe.rentcars.mapper.YearCheckMapper;
import com.cn.yunhe.rentcars.model.Car;
import com.cn.yunhe.rentcars.model.YearCheck;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@RestController
public class YearCheckController {
	
	//增加	URL：annual/add method:post
	//删除	URL：annual/delete method:delete
	//查询	URL：annual/select method:get
	//修改	URL：annual/query method:put
	@Autowired
	private YearCheckMapper yearCheckMapper;
	@Autowired
	private CarMapper carMapper ;
/*	@RequestMapping(value="/cspage", method=RequestMethod.GET)
	public ModelAndView cspage() {	
		return new ModelAndView("cs");
	}*/
	
	@RequestMapping(value="/yearcheck-list", method=RequestMethod.GET)
	public ModelAndView yearCheckList() {	
		return new ModelAndView("yearcheck-list");
	}

	@RequestMapping(value="/yearcheck-add", method=RequestMethod.GET)
	public ModelAndView showAddYearCheck() {	
		return new ModelAndView("yearcheck-add");
	}
	
	@RequestMapping(value="/yearcheck-edit", method=RequestMethod.GET)
	public ModelAndView showEditYearCheck(@RequestParam String recordid) {	
		
		YearCheck yearCheck = yearCheckMapper.getYearCheckById(recordid);
		
		Map<String,YearCheck> modelMap = new HashMap<String,YearCheck>();
		modelMap.put("yearCheck", yearCheck);
		
		return new ModelAndView("yearcheck-edit",modelMap);		
	}
	
	@RequestMapping(value="/getyearcheckbycarid", method=RequestMethod.GET)
	public PageInfo<YearCheck> getYearcheckbycarid(@RequestParam String carid,@RequestParam  int pageNum,@RequestParam  int pageSize){
	      PageHelper.startPage(pageNum, pageSize);
	      List<YearCheck> yearCheckList = yearCheckMapper.getYearCheck(carid);
	      PageInfo<YearCheck> page = new PageInfo<YearCheck>(yearCheckList);
	      return page;
	}
	
	
	/**
	 * 添加新用户
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/addYearCheck", method=RequestMethod.POST)
	public void addYearCheck(@RequestBody YearCheck yearCheck){
		yearCheck.setRecordid(UUID.randomUUID().toString());
		yearCheckMapper.addYearCheck(yearCheck);
		
		Car car = new Car ();
		if(yearCheck.getReturntime() !=""){			
			car.setCarstate(6);
			car.setYearcheck(yearCheck.getNexttime());			
		}else{
			car.setCarstate(10);
		}
		car.setCarcode(yearCheck.getCarid());			
		carMapper.setCarAllstate(car);
	}
	
	/**
	 * 修改用户
	 * 
	 * @param userid
	 */
	@RequestMapping(value="/yearCheck/{recordid}", method=RequestMethod.PUT)
	public void editYearCheck(@PathVariable("recordid") String recordid,@RequestBody YearCheck yearCheck){
		yearCheck.setRecordid(recordid);
		yearCheckMapper.editYearCheck(yearCheck);
		
		if(yearCheck.getReturntime() !=""){
			Car car = new Car ();
			car.setCarstate(6);
			car.setYearcheck(yearCheck.getNexttime());
			car.setCarcode(yearCheck.getCarid());			
			carMapper.setCarAllstate(car);
		}
	}
	

	/**
	 * 编辑用户状态
	 * 
	 * @param userid
	 * @param isabled
	 */
	/*@RequestMapping(value="/setUserState", method=RequestMethod.POST)
	public void handleUserState(@RequestParam String recordid,@RequestParam  int isabled){ 
		YearCheck yearCheck = new YearCheck();
		yearCheck.setId(recordid);
		yearCheck.setIsabled(isabled);
		yearCheckMapper.setUserState(user);
	}*/

	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delYearCheck", method=RequestMethod.POST)
	public void delYearCheck(@RequestParam String recordid){ 
		yearCheckMapper.delYearCheck(recordid);
	}
	
	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delYearChecks", method=RequestMethod.POST)
	public void delYearChecks(@RequestParam String recordid){ 
		List<String> recordidList = Arrays.asList(recordid.split(","));  		
		yearCheckMapper.delYearChecks(recordidList);
	}
	
	
	/**
	 * 搜索查询用户列表信息
	 * 
	 * @param username
	 * @param draw
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value="/yearCheck", method=RequestMethod.GET)
	public Map<String,Object> getLikeYearCheck(@RequestParam String datemin,@RequestParam String datemax,@RequestParam String carid,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		  
		  Map<String,String> params =  new HashMap<String,String>();
		  params.put("carid", carid);
		  params.put("datemin", datemin);
		  params.put("datemax", datemax);
		
		  int pageNum = (start/length)+1;
		  int pageSize = length;		 
		  
	      PageHelper.startPage(pageNum, pageSize);
	      List<YearCheck> yearCheckList = yearCheckMapper.searchYearCheck(params);
	      PageInfo<YearCheck> page = new PageInfo<YearCheck>(yearCheckList);
	      
	      Map<String,Object> rsMap = new HashMap<String,Object>();
	      rsMap.put("draw",draw);
	      rsMap.put("data", page.getList());	      
	      rsMap.put("recordsTotal", page.getTotal());
	      rsMap.put("recordsFiltered", page.getTotal());
	      return rsMap;
	}

}
